package ren.nearby.http.di;


import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.lang.reflect.Type;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

import ren.nearby.http.exception.Api2Exception;


/**
 * Created by wukewei on 16/5/26.
 */
public class RxResultHelper {
    //http://www.jianshu.com/p/b1da0387f805
    public static <T> ObservableTransformer<HttpResultBean<T>, T> handleResult() {
        return new ObservableTransformer<HttpResultBean<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<HttpResultBean<T>> upstream) {
                Logger.e("apply 1=  ");
                return upstream.flatMap(new Function<HttpResultBean<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(@NonNull HttpResultBean<T> tHttpResultBean) throws Exception {
                        Logger.e("apply 2= " + tHttpResultBean.getData());
                       /* if (tHttpResultBean.getStatus() == 200) {
                            return createData(tHttpResultBean.getData());
                        } else {
                            return Observable.error(new Api2Exception(tHttpResultBean.getMsg()));
                        }*/
                        if (tHttpResultBean.getCount() > 0) {
                            return createData(tHttpResultBean.getMusics());
                        } else {
                            return Observable.error(new Api2Exception(tHttpResultBean.getMsg()));
                        }
                    }
                });
            }
        };
    }

    public static <T> ObservableTransformer<HttpResultBean3<T>, T> handleResult2() {
        return new ObservableTransformer<HttpResultBean3<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<HttpResultBean3<T>> upstream) {
                return upstream.flatMap(new Function<HttpResultBean3<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(@NonNull HttpResultBean3<T> tHttpResultBean3) throws Exception {
                        Logger.e("call... ");
                        if (tHttpResultBean3.getREP_BODY().getRESPCOD().equals("000000")) {
                            return createData2((T) tHttpResultBean3.getREP_BODY());
                        } else {
                            return Observable.error(new Api2Exception(tHttpResultBean3.getREP_BODY().getRSPMSG()));
                        }
                    }
                });
            }
        };
    }






    public static <T> ObservableTransformer<HttpResultBean4<T>, T> handleResult5() {
        return new ObservableTransformer<HttpResultBean4<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<HttpResultBean4<T>> upstream) {
                return upstream.flatMap(
                        new Function<HttpResultBean4<T>, ObservableSource<T>>() {
                            @Override
                            public Observable<T> apply(HttpResultBean4<T> result) {
                                Logger.e("apply 4= ");
                                if (result.getREP_HEAD().getRSPCOD().equals("000000")) {
                                    Logger.e("进来了");
                                    return createData(result.getREP_BODY());
                                } else {
                                    return Observable.error(new Api2Exception(result.getREP_HEAD().getRSPMSG()));
                                }
                            }
                        }
                );
            }
        };
    }




    public static <T> Observable<T> createData(final T t) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                try {
                    Logger.e("...................");
                    emitter.onNext(t);
                    emitter.onComplete();
                } catch (Exception e) {
                    emitter.onError(e);
                }
            }
        });
    }

    public static <T> Observable<T> createData2(final T t) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<T> emitter) throws Exception {
                try {
                    emitter.onNext(t);
                    emitter.onComplete();
                } catch (Exception e) {
                    emitter.onError(e);
                }
            }
        });
    }
}

